#include <func.h>
#include"train.h"
int sendn(int fd,const void* p,int len){
    int left = len;
    char * pbuf = (char*)p;
    int ret = 0;
    while(left > 0){
        ret = send(fd,pbuf,len,0);
        printf("ret:%d\n",ret);
        if(ret < 0 ){
            perror("send");
            break;

        }else{
            pbuf+=ret;
            left-=ret;

        }

    }
    return len-left;//返回发送了多长的数据

}

void trainSend(char*buff,int peerfd){
    train_t train;
    memset(&train,0,sizeof(train));
    train.length = strlen(buff)-1;
    strncpy(train.data,buff,train.length);
    int ret = sendn(peerfd,&train,4+train.length);//发送指令
    printf("send buff:%s is success\n",train.data);
    printf("---------------------\n");

}

void splitChar(char* from, char* operate, char* operand){
    int fromLen = strlen(from);
    int i = 0, j = 0;

    for(i = 0;i<fromLen;i++){
        if(from[i]==' '){
            i++;
            break;

        }else{
            operate[i] = from[i];

        }

    }
    printf("split order is %s\n",operate);

    for(j=0;i<fromLen-1;j++,i++){
        operand[j]=from[i];

    }
    printf("split filename is %s\n",operand);
}


int recvn(int sfd, void *p, int len)
{
    char *str = (char *)p;
    int cur = 0, left = len, ret;
    while (left > 0)
    {
        ret = recv(sfd, str + cur, left, 0);
        ERROR_CHECK(ret, -1, "recv");
        cur += ret;
        left -= ret;

    }
    return cur;

}

